package com.example.sm.mycook;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */
public class Address_adapter extends BaseAdapter {
    private List<User> list;
    private Context context;

    public Address_adapter(List<User> list, Context context) {
        this.list = list;
        this.context = context;
    }

    public void AddData(List<User> data) {
        list.clear();
        list.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.address_listview_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mAddressItemName.setText(list.get(position).getName());
        holder.mAddressItemPhone.setText(list.get(position).getPhone());

        if ("家庭".equals(list.get(position).getType())) {
            holder.mAddressItemType.setBackgroundColor(Color.RED);
            holder.mAddressItemType.setText(list.get(position).getType());
        } else if ("公司".equals(list.get(position).getType())) {
            holder.mAddressItemType.setBackgroundColor(Color.GREEN);
            holder.mAddressItemType.setText(list.get(position).getType());
        } else if ("".equals(list.get(position).getType())) {
            holder.mAddressItemType.setBackgroundColor(Color.BLUE);
            holder.mAddressItemType.setText("无");
        }
        holder.mAddressItemAddress.setText(list.get(position).getAddress()+","+list.get(position).getPath());
        if ("1".equals(list.get(position).getCheck())) {
            holder.mAddressItemDefault.setVisibility(View.VISIBLE);
        } else {
            holder.mAddressItemDefault.setVisibility(View.INVISIBLE);
        }
        holder.mAddressItemSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,SettingsActivity.class);
                intent.putExtra("name",list.get(position).getName());
                intent.putExtra("phone",list.get(position).getPhone());
                intent.putExtra("address",list.get(position).getAddress());
                intent.putExtra("path",list.get(position).getPath());
                intent.putExtra("types",list.get(position).getType());
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    class ViewHolder {
        View rootView;
        TextView mAddressItemName;
        TextView mAddressItemPhone;
        TextView mAddressItemType;
        TextView mAddressItemAddress;

        TextView mAddressItemDefault;
        TextView mAddressItemSetting;
        RelativeLayout mSetting;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.mAddressItemName = (TextView) rootView.findViewById(R.id.address_item_name);
            this.mAddressItemPhone = (TextView) rootView.findViewById(R.id.address_item_phone);
            this.mAddressItemType = (TextView) rootView.findViewById(R.id.address_item_type);
            this.mAddressItemAddress = (TextView) rootView.findViewById(R.id.address_item_address);

            this.mAddressItemDefault = (TextView) rootView.findViewById(R.id.address_item_default);
            this.mAddressItemSetting = (TextView) rootView.findViewById(R.id.address_item_setting);
            this.mSetting = (RelativeLayout) rootView.findViewById(R.id.setting);
        }

    }

}
